;layui.define(function (e) {
    layui.use(['index', 'form', 'jquery', 'http', 'common', 'laytpl', 'table'], function () {

        var $ = layui.$
            , http = layui.http
            , form = layui.form
            , common = layui.common
            , laytpl = layui.laytpl;

        var urls = {
            getUserRoleData: "/core/user/role/getUserRoleData?userId=" + $('#userId').val(),
            modifyData: "/core/user/role/modifyData"
        };

        http.get({
            loading: true,
            loadingText: '',
            url: urls.getUserRoleData,
            success: function (res) {
                var getTpl = $('#role').html()
                    , view = $('#roleList');
                laytpl(getTpl).render(res.data, function (html) {
                    view.html(html);
                    form.render('checkbox');
                });

            }
        });
        form.on("submit(modifyData)", function (data) {
            var roleIds = [];
            $('input[type=checkbox]:checked').each(function (i, item) {
                roleIds.push($(item).val());
            });
            data.field['roleId'] = roleIds.join(',');
            http.post({
                loading: true,
                loadingText: '数据提交中,请稍后...',
                url: urls.modifyData,
                data: data.field,
                success: function (res) {
                    parent.fn.refresh(false);
                    common.showSuccessTips('用户角色保存成功!');
                }
            });
            return false;
        });

    });

    e("core/user_role", {});
});